﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Interfaces;

namespace Scheduling
{
    [DataContract]
    public class TimeSpec : ITimeSpec
    {
        [DataMember]
        public Evaluator Evaluator { get; set; }

        [DataMember]
        public string Setting { get; set;}

        public TimeSpec(string setting)
        {
            Setting = Regex.Replace(setting, @"\s+", "");
            if (Regex.IsMatch(Setting, @"(([0-9]+),{1})+[0-9]+") || Regex.IsMatch(Setting, @"([0-9]+)-{0}")) { Evaluator = new ListEvaluator(Setting); }
            else if (Regex.IsMatch(Setting, @">([0-9]+)")) { Evaluator = new GreaterThanEvaluator(Setting); }
            else if (Regex.IsMatch(Setting, @"<([0-9]+)")) { Evaluator = new LessThanEvaluator(Setting); }
            else if (Regex.IsMatch(Setting, @"<>([0-9]+)")) { Evaluator = new NotEqualEvaluator(Setting); }
            else if (Regex.IsMatch(Setting, @"\(([0-9]+)-([0-9]+)\)")) { Evaluator = new RangeEvaluator(Setting, Inclusion.None); }
            else if (Regex.IsMatch(Setting, @"\(([0-9]+)-([0-9]+)\]")) { Evaluator = new RangeEvaluator(Setting, Inclusion.End); }
            else if (Regex.IsMatch(Setting, @"\[([0-9]+)-([0-9]+)\)")) { Evaluator = new RangeEvaluator(Setting, Inclusion.Beginning); }
            else if (Regex.IsMatch(Setting, @"\[([0-9]+)-([0-9]+)\]") || Regex.IsMatch(Setting, @"([0-9]+)-([0-9]+)")) { Evaluator = new RangeEvaluator(Setting, Inclusion.BeginningEnd); }
        }

        public bool Evaluate(int value)
        {
            bool retval = false;
            if (Evaluator != null)
            {
                retval = Evaluator.Evaluate(value);
            }
            return retval;
        }
    }

    public enum Mode
    {
        List,
        GreaterThan,
        LessThan,
        Range,
        None
    }

    public enum Inclusion
    {
        BeginningEnd,
        Beginning,
        End,
        None
    }
}
